package com.cictec.middleware.core.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cictec.middleware.core.entity.Device;
import com.cictec.middleware.core.service.DeviceService;

@RestController
public class DeviceController {

	private static Logger logger = LoggerFactory.getLogger(DeviceController.class);

	@Autowired
	private DeviceService deviceService;

	/**
	 * 根据设备uuid查询设备
	 * 
	 * @param devUuid
	 * @return
	 */
	@RequestMapping(value = "/dc/device/devuuid/{devUuid}")
	public Device selectDeviceByDevUuid(@PathVariable("devUuid")String devUuid) {
		Device device = deviceService.selectDeviceByDevUuid(devUuid);
		return device;
	}

	/**
	 * 根据设备编号查询设备
	 * 
	 * @param devCode
	 * @return
	 */
	@RequestMapping(value = "/dc/device/devCode/{devCode}")
	public Device selectDeviceByDevCode(@PathVariable("devCode")String devCode) {
		return deviceService.selectDeviceByDevCode(devCode);
	}

	/**
	 * 根据车辆uuid查询设备
	 * 
	 * @param busUuid
	 * @return
	 */
	@RequestMapping(value = "/dc/device/busUuid/{busUuid}")
	public Device selectDeviceByBusUuid(@PathVariable("busUuid")String busUuid) {

		return deviceService.selectDeviceByBusUuid(busUuid);
	}

	/**
	 * 根据序列号查询设备编号
	 * 
	 * @param serialNumber
	 * @return
	 */
	@RequestMapping(value = "/dc/device/serialNumber/{serialNumber}")
	public Device selectDeviceByRefId(@PathVariable("serialNumber")String serialNumber) {
		return deviceService.selectDeviceByRefId(serialNumber);

	}

	/**
	 * 更新设备在线状态
	 * 
	 * @param devUuid
	 * @param devOnlineStatus
	 */
	@RequestMapping(value = "/dc/device/updateDeviceOnlineStatusByDevUuid")
	public void updateDeviceOnlineStatusByDevUuid(@RequestParam("devUuid")String devUuid, @RequestParam("devOnlineStatus")String devOnlineStatus) {
		deviceService.updateDeviceOnlineStatusByDevUuid(devUuid, devOnlineStatus);
	}

	/**
	 * 更新设备序列号 <br>
	 * 数据库对应字段 dev_ref_id
	 * 
	 * @param devUuid
	 * @param serialNumber
	 */
	@RequestMapping(value = "/dc/device/updateDeviceSerialNumber")
	public void updateDeviceSerialNumber(@RequestParam("devUuid")String devUuid, @RequestParam("serialNumber")String serialNumber) {
		logger.debug("【设备管理服务】【更新设备序列号】devUuid:{}  serialNumber:{}", devUuid, serialNumber);
		Device device = new Device();
		device.setDevUuid(devUuid);
		device.setDevRefId(serialNumber);
		device.setDevUpdateTime(new Date());
		deviceService.updateDeviceSerialNumber(devUuid, serialNumber);
	}

}
